#from utils.checker.md5 import Md5Checker
from utils.stream.filestream import FileStream
from utils.stream.stream import Stream
from defs.exception import BlaIOError
import unittest
import os

class TestFileStream(unittest.TestCase):
    def setUp(self):
        wfile = FileStream("write.file", writeonly = True)
        wfile.open()
        for i in range(10):
            wfile.write(str(i) * 10)
        #print wfile.tell()
        wfile.close()
        
        
        
    def testAccess(self):
        wfile = FileStream("write.file", writeonly = True)
        # while not open, must fail
        try:
            wfile.write("must fail")
            self.assertFalse(True)
        except BlaIOError:
            self.assertTrue(True)
        
        #afile = open("write.file",  "rw")
        
        wfile.open()
        self.assertEqual(wfile.tell(), 0)
        wfile.write("0123456789")
        self.assertEqual(wfile.tell(), 10)
        wfile.write("0123456789")
        self.assertEqual(wfile.tell(), 20)
        
        wfile.seek(0, os.SEEK_END)
        wfile.write("0123456789")
        self.assertEqual(wfile.tell(), 110)
        self.assertEqual(wfile.getSize(), 110)

        try:
            wfile.read()
            self.assertFalse(True)
        except:
            self.assertTrue(True)
        
        wfile.close()
        
        rfile = FileStream("write.file", readonly = True)
        rfile.open()
        
        self.assertEqual(rfile.tell(), 0)
        content = rfile.read(10)
        self.assertEqual(len(content), 10)
        content = rfile.read()
        self.assertEqual(len(content), 100)
        
        rfile.seek(0, os.SEEK_SET)
        try:
            rfile.write("must fail")
            self.assertFalse(True)
        except:
            self.assertTrue(True)
        
        rfile.close()
        
        
        rwfile = FileStream("write.file")
        rwfile.open()
        
        rwfile.seek(30)
        
        try:
            rwfile.read(30)
            rwfile.write("must success")
        except:
            self.assertFalse(True)
            
        rwfile.close()
        
    def testFileStream(self):
        # get source ready
        rfile = FileStream("write.file", readonly = True)
        rfile.open()
        # get streams ready
        wfile1 = FileStream("write1.file", writeonly = True)
        wfile1.open()
        wfile2 = FileStream("write2.file", writeonly = True)
        wfile2.open()
        # get pipe
        pipe = Stream()
        pipe.open()
        pipe.addOutBranch(wfile1)
        pipe.addOutBranch(wfile2)
        
        while True:
            char = rfile.read(1)
            if char:
                pipe.write(char)
            else:
                break
                
        pipe.close()
        wfile1.close()
        wfile2.close()
        rfile.close()
                
        self.assertTrue(os.path.exists("write1.file"))
        self.assertTrue(os.path.exists("write2.file"))
        os.remove("write1.file")
        os.remove("write2.file")
        
    def tearDown(self):
        pass
        os.remove("write.file")
    
suite = unittest.TestLoader().loadTestsFromTestCase(TestFileStream)
if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(suite)
